package org.balabala.user.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.Page;
import org.balabala.user.dao.FavoriteInfoDao;
import org.balabala.user.entity.BrowseInfoEntity;
import org.balabala.user.entity.FavoriteInfoEntity;
import org.balabala.user.entity.VideoEntity;
import org.balabala.user.feign.VideoFeign;
import org.balabala.user.service.FavoriteInfoService;
import org.balabala.user.utils.PageUtils;
import org.balabala.user.utils.Query;
import org.balabala.user.utils.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import sun.rmi.runtime.Log;


@Service("favoriteInfoService")
public class FavoriteInfoServiceImpl extends ServiceImpl<FavoriteInfoDao, FavoriteInfoEntity> implements FavoriteInfoService {
    @Autowired
    private VideoFeign videoFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FavoriteInfoEntity> page = this.page(
                new Query<FavoriteInfoEntity>().getPage(params),
                new QueryWrapper<FavoriteInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getListByUid(HashMap<String, Object> hashMap) {
        IPage<FavoriteInfoEntity> page = this.page(
                new Query<FavoriteInfoEntity>().getPage(hashMap),
                new QueryWrapper<FavoriteInfoEntity>().eq("uid",hashMap.get("uid")).orderByDesc("favorite_time")
        );
        List<FavoriteInfoEntity> records = page.getRecords();
        List<Long> collect = records.stream().map(item -> item.getVideoId()).collect(Collectors.toList());
        Long[] videoIds = collect.toArray(new Long[]{});
        //调用用户服务，进行视频信息的查询
        Resp resp = videoFeign.listByUid(videoIds);
        List<VideoEntity> data = resp.getData(new TypeReference<List<VideoEntity>>() {
        });
        //
        if(resp.getCode() == 0){
            for(int i=0;i<records.size();i++) {
                records.get(i).setVideoEntity(data.get(i));
            }
        }else{
            return null;
        }
        return new PageUtils(page);

    }

    @Override
    public Resp saveInfo(Long uid, Long videoId) {
        HashMap<String,Long> hashMap = new HashMap<>();
        hashMap.put("uid",uid);
        hashMap.put("video_id",videoId);
        //查询是否已经有喜欢的记录
        FavoriteInfoEntity entity = this.baseMapper.selectOne(new QueryWrapper<FavoriteInfoEntity>().allEq(hashMap));
        if(entity == null){
            FavoriteInfoEntity favoriteInfoEntity = new FavoriteInfoEntity();
            favoriteInfoEntity.setUid(uid);
            favoriteInfoEntity.setVideoId(videoId);
            favoriteInfoEntity.setFavoriteTime(new Date());
            int insert = this.baseMapper.insert(favoriteInfoEntity);
            if(insert == 1){
                return Resp.ok();
            }else {
                return Resp.error(500,"添加失败");
            }
        }else {
            return Resp.error(500,"该视频已添加");
        }
    }


}